<?php
 class SF_AutoCoupon_Model_Lotterycode extends Mage_Core_Model_Abstract {
    const XML_PATH_CONFIRM_EMAIL_IDENTITY       = 'newsletter/subscription/confirm_email_identity';
     
     protected  function  _construct(){
         $this->_init('autocoupon/lotterycode');
     }
     
     public function sendActiveCoupon(){

         if(empty($this->getEmail())||empty($this->getEventsCode())){return false;}
         if(!$this->isSendLotteryCode()){
             return false;
         }
     
         try {
             $this->sendEmailOfLottery();
             return true;
         } catch (Exception $e) {
             return false;
         }
     }
     
     public function sendEmailOfLottery(){
         $templateCodeLang ="";
         $templateCode = $this->getEventsCode();
         $email = Mage::getModel('core/email_template');
         $storeStoreId = Mage::app()->getStore()->getStoreId();
         if($storeStoreId=="2"){
             $templateCodeLang =$templateCode."_ru";
         }else{
             $templateCodeLang =$templateCode;
         }
         $emailTemplate_model  = $email->loadByCode($templateCodeLang);
         if(!$emailTemplate_model ->getId()){
             $templateCodeLang =$templateCode;
             $emailTemplate_model  = $email->loadByCode($templateCodeLang);
         }
         $emailTemplateId =$emailTemplate_model ->getId();
         $translate = Mage::getSingleton('core/translate');
         /* @var $translate Mage_Core_Model_Translate */
         $translate->setTranslateInline(false);
     
         $email->sendTransactional(
             $emailTemplateId,
             Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
             $this->getEmail(),
             $this->getName(),
             array('lotterycode'=>$this)
             );
         $translate->setTranslateInline(true);
     
     }
     public function createLotteryCode(){
         $record=array(
             "email"=> $this->getEmail(),
             "events_code"=>$this->getEventsCode(),
         );
            $collection = $this->getCollection();
            $collection->addFieldToFilter("events_code",$record['events_code']);
            $collection->getSelect()->columns(
                 array('draw_code' => new Zend_Db_Expr('MAX(main_table.draw_code)'))    
               );
           $codeInfo =  $collection->getData();
           if($codeInfo[0]['draw_code']==""){
               $lotterycode = 100001;
           }else{
               $lotterycode = $codeInfo[0]['draw_code']+1;
           }
           $this->setDrawCode($lotterycode);
           $this->setEmail($record['email']);
           $this->setEventsCode($record['events_code']);
           $this->save();
         return $lotterycode;

     }
     
     
     //验证是否具备发送抽奖码的资格
     public function isSendLotteryCode(){
         $sub_email = $this->getEmail();
         $events_code = $this->getEventsCode();
         // $events_model var @SF_PromoteEvents_Helper_Data
         $events_model = Mage::helper('promoteevents')->getEvents($events_code);
         $even_info  = $events_model->getEventsData();
         $from_date = strtotime($even_info->getFromDate());
         $to_date     = strtotime($even_info->getToDate());
         $active       =$even_info->getIsActive();
         $now = Mage::getModel( 'core/date' )->timestamp(time());
         $collection = $this->getCollection();
         $is_subscriber=$collection
         ->addFieldToFilter("events_code",array('eq'=>$events_code))
         ->addFieldToFilter("email",array('eq'=>$sub_email))
         ->getData();
         
         if(!$is_subscriber&&$now>=$from_date&&$now<=$to_date&&$active==1)
         {
             return true;
         }
         return false;
     }
 }